import tkinter as tk
import redis
import os
from util import get_delete_data_from_redis
import tkinter.font as tkFont
import json

r = redis.Redis(host='localhost', port=6379, db=0)

# 创建一个订阅者对象
p = r.pubsub()

# 订阅频道
p.subscribe('door_status')


def ui_color():

    top_color = "#2c5cb3"
    mid_color = "#3399FF"

    if os.path.exists("/home/linaro/.ui_color.json"):

        with open("/home/linaro/.ui_color.json", encoding="utf-8") as f:

            return json.loads(f.read())
        
    else:

        return {
            "top_color": "#2c5cb3",
            "mid_color": "#3399FF"
        }
            





def update_data():
    # 更新数据源
    status = r.get("cabinet_current_status")
    status = status.decode() if status else ""

    cabinet_current_order_status = r.get("cabinet_current_order_status")

    if cabinet_current_order_status==None:
        cabinet_current_order_status = ""
    else:
        cabinet_current_order_status = cabinet_current_order_status.decode()

    print(cabinet_current_order_status)

    if cabinet_current_order_status == "补货":

        label_product_top.config(text="正在补货")

    elif cabinet_current_order_status == "购物":

        label_product_top.config(text="正在选购")

    elif cabinet_current_order_status == "无任务":

        label_product_top.config(text="无任务")

    else:

        label_product_top.config(text="无任务")
    
    if status == "盘点结束":
        # 从数据源获取新数据（例如从Redis中获取数据）
        data = get_delete_data_from_redis()  # 替换为适当的数据源函数

        if cabinet_current_order_status == "补货":

            label_total.config(text="补货完成")

        elif cabinet_current_order_status == "购物":

            # label_total.config(text="选购总数量：{}".format(len(data)))
            label_total.config(text="选购总数量：{}".format(len(data)))

        elif cabinet_current_order_status == "无任务":

            label_total.config(text="欢迎选购")

        else:

            label_total.config(text="欢迎选购")


        if len(data) != 0 and cabinet_current_order_status =="购物":

            label_product.config(text = "蒙山毛峰")
            label_product_quantity.config(text = "{} 个".format(str(len(data))))

        else:

            label_product.config(text = "--")
            label_product_quantity.config(text = "--")


    else:

        label_total.config(text="当前设备状态：{}".format(status))

        label_product.config(text = "--")
        label_product_quantity.config(text = "--")


    # 重新调用更新函数，实现定期更新
    root.after(1000, update_data)  # 每1秒钟更新一次

root = tk.Tk()
root.attributes('-fullscreen', True)

# 获取屏幕的高度
screen_height = root.winfo_screenheight()

# 计算每块的高度，假设有4块，并且按比例分配
block_height = screen_height // 10

# 设置字体
# font_style_content = ('黑体', 14)
# font_style = ('黑体', 28)

color_data = ui_color()
top_color = color_data["top_color"]
mid_color = color_data["mid_color"]

print(color_data)

font_style_content = tkFont.Font(family="AR PL UMing CN", size=20)
font_style = tkFont.Font(family="AR PL UKai CN", size=28)

# 上部分
frame_top = tk.Frame(root, bg=top_color, height=block_height)
frame_top.pack(side="top", fill="both", expand=True)

label_product_top = tk.Label(frame_top, text="选购商品", bg=top_color, fg="white", font=font_style)
label_product_top.pack(side="left", padx=50,  expand=True, fill="x")

# label_quantity = tk.Label(frame_top, text="数量", bg="#0000CC", fg="white", font=font_style)
# label_quantity.pack(side="left", padx=350,  expand=True, fill="x")
frame_top.pack_propagate(False)

# 中间部分
frame_center = tk.Frame(root, bg=mid_color, height=block_height * 7)
frame_center.pack(side="top", fill="both", expand=True)  # 添加适当的填充
frame_center.pack_propagate(False)

frame_product = tk.Frame(frame_center, bg=mid_color)
frame_product.pack(side="top", fill="both", expand=True)


label_product = tk.Label(frame_product, text="无数据", bg=mid_color, fg="black", font=font_style_content)
label_product.pack(side="left", padx=50)

label_product_quantity = tk.Label(frame_product, text="", bg=mid_color, fg="black", font=font_style_content)
label_product_quantity.pack(side="left")


# 放置视频
# video_label = tk.Label(frame_center, text="这里放视频", bg="red", fg="black", font=font_style_content)
# video_label.pack(side="right", padx=50, pady=block_height // 2)  # 添加适当的填充

# 下部分
frame_bottom = tk.Frame(root, bg=top_color, height=block_height*2)
frame_bottom.pack(side="top", fill="both", expand=True)
frame_bottom.pack_propagate(False)
label_total = tk.Label(frame_bottom, text="总数量：00", bg=top_color, fg="white", font=font_style)
label_total.pack(side="left", padx=100)


# 调用更新函数，开始定期更新
update_data()

root.mainloop()